home *** CD-ROM | disk | FTP | other *** search
/ Programming in Microsoft Windows with C# / Programacion en Microsoft Windows con C#.iso / Codigo / Vista en árbol y vista en lista / ImageDirectory / ImagePanel.cs < prev   
Encoding:
Text File  |  2002-05-30  |  4.4 KB  |  157 lines

  1. //-----------------------------------------
  2. // ImagePanel.cs ⌐ 2001 by Charles Petzold
  3. //-----------------------------------------
  4. using System;
  5. using System.Drawing;
  6. using System.IO;
  7. using System.Windows.Forms;
  8.  
  9. class ImagePanel: Panel
  10. {
  11.      const int cxButton = 100, cyButton = 100;         // Tama±o del bot≤n de imagen
  12.  
  13.      Button    btnClicked;
  14.      ToolTip   tooltip = new ToolTip();
  15.      Timer     timer = new Timer();
  16.  
  17.           // Campos para el evento Tick de Timer
  18.  
  19.      string[]  astrFileNames;
  20.      int       i, x, y;
  21.                                                        // Evento p·blico
  22.      public event EventHandler ImageClicked;
  23.                                                        // Constructor
  24.      public ImagePanel()
  25.      {
  26.           AutoScroll = true;
  27.  
  28.           timer.Interval = 1;
  29.           timer.Tick += new EventHandler(TimerOnTick);
  30.      }
  31.                                                        // Propiedades p·blicas
  32.      public Control ClickedControl
  33.      {
  34.           get { return btnClicked; }
  35.      }
  36.      public Image ClickedImage
  37.      {
  38.           get 
  39.           { 
  40.                try
  41.                {
  42.                     return Image.FromFile((string) btnClicked.Tag);
  43.                }
  44.                catch
  45.                {
  46.                     return null;
  47.                }
  48.           }
  49.      }
  50.                                                        // MΘtodo p·blico
  51.      public void ShowImages(string strDirectory)
  52.      {
  53.           Controls.Clear();
  54.           tooltip.RemoveAll();
  55.  
  56.           try
  57.           {
  58.                astrFileNames = Directory.GetFiles(strDirectory);
  59.           }
  60.           catch
  61.           {
  62.                return;
  63.           }
  64.  
  65.           i = x = y = 0;
  66.  
  67.           timer.Start();
  68.      }
  69.                                                        // Manejadores de evento
  70.      void TimerOnTick(object obj, EventArgs ea)
  71.      {
  72.           Image image;
  73.  
  74.           if (i == astrFileNames.Length)
  75.           {
  76.                timer.Stop();
  77.                return;
  78.           }
  79.           try
  80.           {
  81.                image = Image.FromFile(astrFileNames[i]);
  82.           }
  83.           catch
  84.           {
  85.                i++;
  86.                return;
  87.           }
  88.           int cxImage = image.Width;
  89.           int cyImage = image.Height;
  90.  
  91.                // Convertir imagen a tama±o peque±o para el bot≤n.
  92.  
  93.           SizeF sizef = new SizeF(cxImage / image.HorizontalResolution,
  94.                                   cyImage / image.VerticalResolution);
  95.  
  96.           float fScale = Math.Min(cxButton / sizef.Width, 
  97.                                   cyButton / sizef.Height);
  98.           sizef.Width *= fScale;
  99.           sizef.Height *= fScale;
  100.           Size size = Size.Ceiling(sizef);
  101.           Bitmap bitmap = new Bitmap(image, size);
  102.           image.Dispose();
  103.  
  104.                // Crear bot≤n y a±adir al panel.
  105.  
  106.           Button btn   = new Button();
  107.           btn.Image    = bitmap;
  108.           btn.Location = new Point(x, y) + (Size) AutoScrollPosition;
  109.           btn.Size     = new Size(cxButton, cyButton);
  110.           btn.Tag      = astrFileNames[i];
  111.           btn.Click   += new EventHandler(ButtonOnClick);
  112.           Controls.Add(btn);
  113.  
  114.                // Dar una Informaci≤n en pantalla al bot≤n
  115.  
  116.           tooltip.SetToolTip(btn, String.Format("{0}\n{1}x{2}",
  117.                                         Path.GetFileName(astrFileNames[i]), 
  118.                                         cxImage, cyImage));
  119.  
  120.                // Ajustar i, x, e y para la imagen siguiente.
  121.  
  122.           AdjustXY(ref x, ref y);
  123.           i++;
  124.      }
  125.      void ButtonOnClick(object obj, EventArgs ea)
  126.      {
  127.           btnClicked = (Button) obj;
  128.  
  129.           if (ImageClicked != null)
  130.                ImageClicked(this, EventArgs.Empty);
  131.      }
  132.      protected override void OnResize(EventArgs ea)
  133.      {
  134.           base.OnResize(ea);
  135.  
  136.           AutoScrollPosition = Point.Empty;
  137.           int x = 0, y = 0;
  138.  
  139.           foreach (Control cntl in Controls)
  140.           {
  141.                cntl.Location = new Point(x, y) + (Size) AutoScrollPosition;
  142.                AdjustXY(ref x, ref y);
  143.           }
  144.      }
  145.      void AdjustXY(ref int x, ref int y)
  146.      {
  147.           y += cyButton;
  148.  
  149.           if (y + cyButton > Height - 
  150.                               SystemInformation.HorizontalScrollBarHeight)
  151.           {
  152.                y = 0;
  153.                x += cxButton;
  154.           }
  155.      }
  156. }
  157.